package com.shejimoshi.单例模式.单例模式的安全性.反射攻击;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Main {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Singleton singletonOne = Singleton.getInstance();
        Singleton singletonTwo = Singleton.getInstance();
        Constructor<Singleton> constructor = Singleton.class.getDeclaredConstructor();
        constructor.setAccessible(true);
        Singleton reflectionSingleton = constructor.newInstance();

        System.out.println(singletonOne + "\n" + singletonTwo + "\n" + reflectionSingleton);
        System.out.println("正常情况下，实例化两个实例是否相同："+(singletonOne==singletonTwo));
        System.out.println("通过反射攻击单例模式情况下，实例化两个实例是否相同："+(singletonOne==reflectionSingleton));
    }
}
